package com.woniu.airent.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.airent.dto.ArticleDto;
import com.woniu.airent.dto.ArticleReportDto;
import com.woniu.airent.dto.ReplyDto;
import com.woniu.airent.dto.ReportDto;
import com.woniu.airent.mapper.ArticleMapper;
import com.woniu.airent.mapper.ReplyMapper;
import com.woniu.airent.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    ReplyMapper replyMapper;

    @Override
    public PageInfo<ArticleDto> query(int currentPage, int pageSize, String keyWord,int status) {
        PageHelper.startPage(currentPage,pageSize);
        List<ArticleDto> article = articleMapper.selectArticle(keyWord,status);
        PageInfo<ArticleDto> pageInfo = new PageInfo<>(article);

        return pageInfo;
    }

    @Override
    @Transactional
    public int addArticle(ArticleDto article) {
        int result = articleMapper.insert(article);
        return result;
    }

    @Override
    public ArticleDto singleQuery(int id) {
        return articleMapper.selectSingle(id);
    }

    @Override
    @Transactional
    public int addReply(ReplyDto replyDto) {
        return replyMapper.insertReply(replyDto);
    }

    @Override
    @Transactional
    public int addReport(ArticleReportDto articleReport) {
        int aResult = articleMapper.updataReportStatus(articleReport.getArticleId());
        int rREsult = articleMapper.insertReportRecord(articleReport);
        int result = 0;
        if (aResult!=0&&rREsult!=0){
            result = 1;
        }
        return result;
    }

    @Override
    public List<ArticleReportDto> showReported(int articleId) {
        List<ArticleReportDto> reportDtoList =  replyMapper.selectReported(articleId);
        return reportDtoList;
    }

    @Override
    public PageInfo<ReplyDto> showOneUserReported(Integer userId,String keyWord) {
        PageHelper.startPage(1,3);
        List<ReplyDto> reportDtoList =  replyMapper.selectOneUserReported(userId,keyWord);
        PageInfo<ReplyDto> reportDtoPageInfo = new PageInfo<>(reportDtoList);
        return reportDtoPageInfo;
    }

    @Override
    public int delReply(int replyId) {
        int result = replyMapper.delReply(replyId);
        return 0;
    }
}
